package cn.liccshop.core.service.shopOrderReturn.controller;


import cn.liccshop.core.entity.ShopOrderInfo;
import cn.liccshop.core.entity.ShopOrderReturn;
import cn.liccshop.core.service.shopOrderInfo.service.IShopOrderInfoService;
import cn.liccshop.core.service.shopOrderReturn.service.IShopOrderReturnService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import cn.liccshop.sys.base.MyActionController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @description  前端控制器
 *
 * @author LiCC
 * @since 2019-05-16
 */
@RestController
@RequestMapping("/shopOrderReturn")
public class ShopOrderReturnController extends MyActionController<ShopOrderReturn> {

    @Autowired
    private IShopOrderReturnService liShopOrderReturnService;
    @Autowired
    private IShopOrderInfoService orderInfoService;

    @Override
    protected IService<ShopOrderReturn> getLiService(){
        return liShopOrderReturnService;
    }

    /**
     * 订单退货申请处理
     * @param map
     * @return
     * @throws MySystemException
     */
    @RequestMapping("/editOrder")
    public Result dataUpdateById(@RequestBody Map<String, Object> map) throws MySystemException {
        String id = ObjectToOtherUtils.toString(map.get("id"));
        String processInformation = ObjectToOtherUtils.toString(map.get("processInformation"));
        String orderId = ObjectToOtherUtils.toString(map.get("orderId"));
        Integer orderStatus = ObjectToOtherUtils.toInteger(map.get("orderStatus"));
        if (StringUtils.isBlank(id)||StringUtils.isBlank(processInformation)||StringUtils.isBlank(orderId)){
            /** 参数异常 **/
            throw new MySystemException("参数异常", Constants.INVALID_PARM);
        }
        liShopOrderReturnService.updateById(new ShopOrderReturn().setId(id).setProcessInformation(processInformation));
        orderInfoService.updateById(new ShopOrderInfo().setId(orderId).setOrderStatus(orderStatus).setReturnTime(LocalDateTime.now()));
        return new Result().setCode(Constants.OK).setMessage("更新成功");
    }


    @RequestMapping("/save")
    public Result saveInfo(@RequestBody Map<String, Object> map) throws MySystemException {
        String returnNote = ObjectToOtherUtils.toString(map.get("returnNote"));
        String orderId = ObjectToOtherUtils.toString(map.get("orderId"));
        if (StringUtils.isBlank(returnNote)){
            throw new MySystemException("填写退货原由",Constants.INVALID_PARM);
        }
        liShopOrderReturnService.saveInfo(new ShopOrderReturn().setReturnNote(returnNote).setOrderId(orderId));
        return new Result().setCode(Constants.OK).setMessage("申请退货成功");
    }
}

